const app = getApp()
Page({
  data: {
    bank: {
      money: 0,
    },
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBank();
  },
  /**
   * 读取我的账单
   */
  getBank: function () {
    var param = {
      'signkey': app.util().getRandom(12)
    }
    app.api().Get("api/v4/allwin/bank/index", param, (result) => {
      if (result.code == 200) {
        this.setData({
          bank: result.data
        });
      }
    })
  },
  /**
   * 申请提现
   */
  onSubmit: function (e) {
    let that = this;
    var data = e.detail.value;
    var isPost = true;
    if (app.util().isNull(data.money)) {
      wx.showModal({
        content: '转出金额必须填写'
      })
      return
    } else if (app.util().isNull(data.safepassword)){
      wx.showModal({
        content: '安全密码必须填写'
      })
      return
    } else {
      if (!(/^(([1-9]\d*)(\.\d{1,3})?)$|^(0\.0?([1-9]\d?))$/.test(data.money))) {
        wx.showModal({
          content: '金额输入错误'
        })
        return
      } else {
        var money = parseFloat(data.money);
        if (money > this.data.bank.money || money == 0) {
          wx.showModal({
            content: '您的账户余额不足'
          })
          return
        }
      }
    }
    //提交数据
    wx.showLoading({
      title: '提交申请中',
      mask: true
    })
    var parms = {
      money: data.money,
      safepassword: data.safepassword,
    }
    app.api().Post('api/v4/allwin/bank/cash', parms, function (res) {
      wx.hideLoading();
      if (res.code == 200) {
        app.wxAlert(res.msg,()=>{
          wx.navigateBack({
            delta: 1
          })
        })
      }
    })
  }
})